package com.springapi.service.impl;

import com.springapi.dao.GoodsmanagerDao;
import com.springapi.entity.Goodsmanager;
import com.springapi.entity.Orderlist;
import com.springapi.dao.OrderlistDao;
import com.springapi.service.OrderlistService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单明细表(Orderlist)表服务实现类
 *
 * @author makejava
 * @since 2024-12-31 14:13:37
 */
@Service("orderlistService")
public class OrderlistServiceImpl implements OrderlistService {
    @Resource
    private OrderlistDao orderlistDao;

    @Resource
    private GoodsmanagerDao goodsmanagerDao;

    @Override
    public List<Orderlist> getorderlistbyday(int appid, int shopid, Date createdate1, Date createdate2) {
        return orderlistDao.getorderlistbyday(appid,shopid,createdate1,createdate2);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Orderlist queryById(Integer id) {
        return this.orderlistDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderlist 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Orderlist> queryByPage(Orderlist orderlist, PageRequest pageRequest) {
        long total = this.orderlistDao.count(orderlist);
        return new PageImpl<>(this.orderlistDao.queryAllByLimit(orderlist, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orderlist 实例对象
     * @return 实例对象
     */
    @Override
    public Orderlist insert(Orderlist orderlist) {

        this.orderlistDao.insert(orderlist);
        return orderlist;
    }

    @Override
    public String insertBatchorder(List<Orderlist> orderlists) {
        String res="失败";
        try {
            for (Orderlist orderlist : orderlists) {
               // System.out.println(orderlist.getPaynumber());
                //根据用户下单的商品数量，将商品表中的库存减去
                Goodsmanager goodsmanager =goodsmanagerDao.queryById(orderlist.getGoodsmanagerid());
                goodsmanager.setInventory(goodsmanager.getInventory() - orderlist.getPaynumber());
                goodsmanagerDao.update(goodsmanager);

            }
            this.orderlistDao.insertBatch(orderlists);
            res="成功";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return res;
    }


    /**
     * 修改数据
     *
     * @param orderlist 实例对象
     * @return 实例对象
     */
    @Override
    public Orderlist update(Orderlist orderlist) {
        this.orderlistDao.update(orderlist);
        return this.queryById(orderlist.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.orderlistDao.deleteById(id) > 0;
    }
}
